//
//  NSString+PYUploadVideoByQiniu.m
//  catuncel
//
//  Created by Jim on 2017/12/6.
//  Copyright © 2017年 Jim. All rights reserved.
//

#import "NSString+PYUploadVideoByQiniu.h"

#import "NSString+PYCompressVideo.h"

#import <QiniuSDK.h>

@implementation NSString (PYUploadVideoByQiniu)

- (void)uploadVideoByQiniuWithoutCompress:(PYUploadVideoByQiniuCompletion)completion
{
    NSString *URLString = [NSString stringWithFormat:@"%@/%@/%@", PY_GET_Header, PY_URI_VideoUploadToken, [NSString valueByKey:kCurrentUserID]];

    // 上传视频
    [PYNetworking networkingWithURLString:URLString parameters:nil requestType:PY_GET success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSString *token = responseObject[@"data"][@"token"];
        
        NSString *originalKey = responseObject[@"data"][@"originalKey"];
        
        if (token) {

            QNUploadOption *option = [[QNUploadOption alloc] initWithProgressHandler:^(NSString *key, float percent) {
                
                NSLog(@"video_percent = %f", percent);
                
                py_dispatch_async_main_safe(^{
                    
                    [[NSNotificationCenter defaultCenter] postNotificationName:PYUploadVideoProgressNotification object:[NSNumber numberWithFloat:percent]];
                });
                
            }];
            
            QNUploadManager *manager = [QNUploadManager new];
            
            [manager putFile:self key:originalKey token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
                
                if (completion) {
                    
                    completion(originalKey, nil);
                }
                
            } option:option];
            
            return;
        }
        
        if (completion) {
            
            NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:400 userInfo:@{@"key":@"服务器异常"}];
            
            completion(nil, error);
            
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (completion) {
            
            completion(nil, error);
            
        }
    }];
}

- (void)uploadVideoByQiniu:(PYUploadVideoByQiniuCompletion)completion
{
    NSString *URLString = [NSString stringWithFormat:@"%@/%@/%@", PY_GET_Header, PY_URI_VideoUploadToken, [NSString valueByKey:kCurrentUserID]];
    
    // 上传视频
    [PYNetworking networkingWithURLString:URLString parameters:nil requestType:PY_GET success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSString *token = responseObject[@"data"][@"token"];
        
        NSString *originalKey = responseObject[@"data"][@"originalKey"];
        
        if (token) {
            
            [self compressVideo:^(NSString * _Nonnull videoUrl) {
                
                QNUploadOption *option = [[QNUploadOption alloc] initWithProgressHandler:^(NSString *key, float percent) {
                    
                    NSLog(@"video_percent = %f", percent);
                    
                    py_dispatch_async_main_safe(^{
                        
                        [[NSNotificationCenter defaultCenter] postNotificationName:PYUploadVideoProgressNotification object:[NSNumber numberWithFloat:percent]];
                    });
                    
                }];
                
                QNUploadManager *manager = [QNUploadManager new];

                [manager putFile:videoUrl key:originalKey token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
                    
                    if (completion) {
                        
                        completion(originalKey, nil);
                    }
                    
                } option:option];
            }];
            
            return;
        }
        
        if (completion) {
            
            NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:400 userInfo:@{@"key":@"服务器异常"}];

            completion(nil, error);

        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (completion) {
            
            completion(nil, error);

        }
    }];
}

@end
